<script>
/*
* udpate the items in tank monitor MFG drop down list when changing
* a new tank monitor model
*/
function updateModels(data)
{
	var element = document.getElementById('Tanks_tank_monitor_ID');
	for(var i=element.options.length-1; i>=0; i--)
	{
		element.remove(i);
	}
	for(key in data)
	{
		var option = document.createElement('OPTION');
		option.value = key;
		option.text = data[key];
		element.options.add(option);
	}
}

/*
* udpate the items in product drop down list when changing a new product
* category
*/
function updateProducts(data)
{
	var element = document.getElementById('Tanks_product_ID');
	for(var i=element.options.length-1; i>=0; i--)
	{
		element.remove(i);
	}
	for(key in data)
	{
		var option = document.createElement('OPTION');
		option.value = key;
		option.text = data[key];
		element.options.add(option);
	}
}

function deleteTank(data)
{
	if(data=='delete-tank-success')
	{
		$('#msg-header').show();
		$('#msg-header').html('<b>\''+selectedTankName+'\'</b> has been removed.');

		clearTank();
		displayTank(false); //hide tank details
		
		var productLI=selectedElement.parentNode;//the item with <ul> tag
		var productUL=productLI.parentNode;//the item with <li> tag
		productUL.removeChild(productLI);
		if(productUL.children.length==0)//delete the category if no products are under it
		{
			var categoryLI=productUL.parentNode;
			var productlistArea=categoryLI.parentNode;
			productlistArea.removeChild(categoryLI);
		}
			
	}
}

/*
* confirm with client if deleting the tank
* send delete request if yes
*/
function clickOnDelete()
{
	var message='';
	if(selectedTankID==0)
	{
		message='You need to select a tank.';
	}
	else
	{
		message='Are you going to delete product "'+selectedTankName+'"?';
	}
	var toDelete=window.confirm(message);
	if(toDelete)
	{
		if(selectedTankID==0)
			return false;
		
		$.ajax({
	       url: '<?php echo CController::createUrl('mySites/deleteTank');?>',
	       type: 'post',
	       dataType: 'json',
	       data: {tank_ID:selectedTankID},
	       success: deleteTank
	     });
	}
	selectedTankID=0;
	return false;
}

//clears Tank fields
function clearTank() {
	$("#tank-details input").val('');
}
</script>

<?php $tank_form = $this->beginWidget('CActiveForm', array(
		'id'=>'tank-form',
		'enableClientValidation'=>true,
	//	'enableAjaxValidation'=>true,
		));
?>

<?php echo CHtml::hiddenField('tank_ID_field', '0'); ?>

<div id="sp-right">

<div id="sp-right-head">tank details</div>
<ul class="td-buttons">
        <li><a href="" onclick="return clickOnDelete()"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/delete.jpg" alt="" /></a></li>
</ul><!-- End td buttons -->


<div id="tank-details">
<div align='left' style='color:#FF0000'>
<?php
	echo $tank_form->error($tank, 'tank_name');
//	echo $tank_form->error($tank, 'capacity');
?>
<label id='capacity-label'></label>
</div>
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="38">Tank Id</th>
    <td width="170">
    	<div class="textboxsb6">
    		<?php echo $tank_form->textField($tank, 'tank_ID', array('disabled'=>true)); ?>
    	</div>
    </td>
    <th width="40">Site Id</th>
    <td>
    	<div class="textboxsb6">
    		<?php echo $tank_form->textField($tank, 'site_ID', array('disabled'=>true)); ?>
    	</div>
    </td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="38">Tank NAME</th>
    <td width="153">
    	<div class="textboxsb7">
    		<?php echo $tank_form->textField($tank, 'tank_name', array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    	</div>
    </td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="38">Product<br /> category</th>
    <td>
    	<div class="sp-textbox9">
    		<?php echo CHtml::dropDownList('product_category', '', ProductTypes::model()->getProductCategoryInfo(), array(
												'onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)',
												'ajax'=>array(
														'type'=>'POST',
														'url'=>CController::createUrl('myProducts/products'),
														'success'=>'updateProducts',
														'dataType'=>'json',
													)
												)); ?>
    	</div>
    </td>
    <th width="38">Product</th>
    <td>
    	<div class="sp-textbox4">
    		<?php echo $tank_form->dropDownList($tank, 'product_ID', Products::model()->getProductsInfo(), array(
												'onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)',
												)); ?>
    	</div>
    </td>
  </tr>
  <tr>
    <th width="38">Type</th>
    <td width="185"><div class="sp-textbox9">
    	<?php echo $tank_form->dropDownList($tank, 'tank_type_ID', TankTypes::model()->getTypesInfo(), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div></td>
    <th width="67">Orientation</th>
    <td><div class="sp-textbox4">
    	<?php echo $tank_form->dropDownList($tank, 'tank_position_ID', TankPositions::model()->getPositionsInfo(), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div></td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="52">Capacity</th>
    <td width="178">
    	<div class="textboxsb6">
    		<?php echo $tank_form->textField($tank, 'capacity', array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    	</div>
    </td>
    <th width="58">Unit Type</th>
    <td>
    	<div class="sp-textbox4">
          <?php echo CHtml::dropDownList('unit_type', '1', array("1"=>'GALLONS', "0.26417"=>'LITERS'), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
      	</div>
    </td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="102" >Tank <br />Moniter Mfg</th>
    <td><div class="sp-textbox7">
			<?php echo $tank_form->dropDownList(TankMonitors::model(), 'monitor_MFG', TankMonitors::model()->getMonitorMFG(), array(
													'onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)',
													'ajax'=>array(
														'type'=>'POST',
														'url'=>CController::createUrl('mySites/models'),
														'success'=>'updateModels',
														'dataType'=>'json',
													)
												));
			?>
        </div>
      </td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="102" >Tank Monitor Model</th>
    <td><div class="sp-textbox7">
    	<?php echo $tank_form->dropDownList($tank, 'tank_monitor_ID', array(), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div>
      </td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="96" style="text-align:right; padding-right:6px;">Remote Fill?</th>
    <td width="90"><div class="sp-textbox6">
		<?php echo $tank_form->dropDownList($tank, 'remote_fill', array(1=>'YES', 0=>'NO'), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div></td>
    <th width="45">Pump?</th>
    <td><div class="sp-textbox6">
        <?php echo $tank_form->dropDownList($tank, 'pump_provided', array(1=>'YES', 0=>'NO'), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div></td>
  </tr>
</table>
</div>

<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="231" style="padding-right:6px; text-align:right;">Overfill alarm?</th>
    <td><div class="sp-textbox6">
        <?php echo $tank_form->dropDownList($tank, 'overfill_alarm', array(1=>'YES', 0=>'NO'), array('onchange'=>'changeStatus(DOMAIN.TANK, STATUS.CHANGED)')); ?>
    </div></td>
  </tr>
</table>
</div>

</div><!-- End Tank Details -->

</div><!-- End Sp Right -->
<div id="ms-submit">
		<?php echo CHtml::imageButton(Yii::app()->request->baseUrl.'/images/submit5.jpg', array(
										'ajax'=>array(				
											'type'=>'POST',
											'url'=>CController::createUrl('mySites/tank'),
											'success'=>'updateTankStatus',
											'dataType'=>'json',
										),
									)
								); ?>
</div>

<?php $this->endWidget(); ?>